use "$data\2_firm_ano_perfvars_individual_paper.dta", clear  
	
bys id : egen costs_labour_wins5_2021_m = max(costs_labour_wins5_2021)
keep if wave==3 & panel_2==1 
	
merge 1:1 id wave using "$data\2_firm_regressions_individual_paper.dta", keepusing(strata_all_coll)
keep if _merge == 3


* Controls
global control i.broad_sector i.ag_ano 


* SEs & strata
global se_cs ", a(strata_all_coll) r"
global se_2 ", a(strata_all_coll) r"
global se_3 ", a(strata_all_coll) r"
global se_4 "i.wave, a(strata_all_coll) vce(cl id)"
		
* First part (Overall effects)
eststo clear
local c=1 
foreach o of global outcomes_firm {
    local d=`c'+1
    eststo m`c'`x': areg `o' assignment `o'_base i.c1_`o' i.c2_`o' $control mis_`o'  ${se_cs} 
    qui summ `o' if assignment==0
    local mu : di %5.2f r(mean)
    estadd local mu `mu'
    local ++c 
	
		* 95 % confidence interval for assignment coefficient
		local coef = _b[assignment]
		local se   = _se[assignment]
		local lb   = `coef' - 1.96*`se'
		local ub   = `coef' + 1.96*`se'
		local lb_fmt : display %5.2f `lb'
		local ub_fmt : display %5.2f `ub'
		local ci "[`lb_fmt', `ub_fmt']"
		estadd local ci "`ci'"
	
}



	use "$data\firm_emppanel.dta", clear 
	keep if wave == 9 & emp_extreme==0 
	drop broad_sector 

	preserve
	use "$data\2_firm_ano_perfvars_individual_paper.dta", clear

	duplicates drop id, force
	keep id strata_all_coll broad_sector
	tempfile temp
	save `temp'
	restore
	
	merge 1:1 id  using `temp'
	keep if _merge == 3
	

	
	
	// Run regressions in loop 
	local c=1 
	foreach o of global outcomes_firm_2  {
		
		local d=`c'+1
		
		* ITT estimate 
		eststo n`c'`x': areg `o' assignment `o'_2021_std $control mis_`o' ${se_cs}
		qui summ `o' if assignment==0
		local mu : di %5.2f r(mean)
		estadd local mu `mu'
		
		* 95 % confidence interval for assignment coefficient
		local coef = _b[assignment]
		local se   = _se[assignment]
		local lb   = `coef' - 1.96*`se'
		local ub   = `coef' + 1.96*`se'
		local lb_fmt : display %5.2f `lb'
		local ub_fmt : display %5.2f `ub'
		local ci "[`lb_fmt', `ub_fmt']"
		estadd local ci "`ci'"
	
	
		local ++c 
	}
	
esttab m* n* using "$results\01_tables\Table_4_results_firm.tex", ///
    keep(assignment) ///
    b(%5.2f) se(%5.2f) ///
    star(* 0.10 ** 0.05 *** 0.01) ///
    stats(ci mu N , fmt(%9.3f %9.2f %9.0f %s) ///
          labels("95\% CI" "Mean" "\(N\)" "95\\% CI")) ///
    mtitle("TH. USD" "IHS" "TH. USD" "IHS" "TH. USD" "IHS"             ///
           "TH. USD" "IHS" "TH. USD" "IHS" "Emp." "Emp.(Log)") ///
    mgroups("Revenue" "Labor Productivity" "Labor Costs"               ///
            "Labor Costs p.c." "Profits" "Employment",                 ///
            pattern(1 0 1 0 1 0 1 0 1 0 1 0)                           ///
            prefix(\multicolumn{2}{c}{) suffix(}) span                 ///
            erepeat(\cmidrule(lr){@span}))                             ///
    prehead("\begin{tabular}{l*{10}{c}}\hline\hline"                   ///
            "\multicolumn{1}{l}{\textbf{Panel A: Overall Effect}} \\") ///
    prefoot("\hline \\") ///
    postfoot("") ///
    replace
	*
	
	
* Second part (By firm size)
use "$data\2_firm_ano_perfvars_individual_paper.dta", clear  
merge 1:1 id wave using "$data\2_firm_regressions_individual_paper.dta", keepusing(strata_empcat_alt)

bys id : egen costs_labour_wins5_2021_m = max(costs_labour_wins5_2021)
keep if wave==3 & panel_2==1 

local outcomes "revenue_wins5 ihsrev  prod_labour_wins5 prl costs_labour_wins5  ihscosts_labour lc_pc_wins5 ihslc_pc profit_wins5 ihsprof"
eststo clear
foreach o of local outcomes {
	
	
    // Run the interacted regression
			if inlist("`o'", "revenue_wins5",  "ihsrev",  "prod_labour_wins5" , "costs_labour_wins5", "ihscosts_labour", "lc_pc_wins5", "ihslc_pc", "profit_wins5",  "ihsprof" ) {
		areg `o' i.assignment##i.empcat `o'_base i.c1_`o' i.c2_`o' i.broad_sector i.ag_ano mis_`o', a(strata_empcat_alt) vce(cl id)
	    eststo m_`o': margins empcat, dydx(1.assignment) post coeflegend
			}
			
				if inlist("`o'", "prl" ) {

		areg `o' i.assignment##i.empcat `o'_base  i.c2_`o' i.broad_sector i.ag_ano mis_`o',  a(strata_empcat_alt) vce(cl id)
	    eststo m_`o': margins empcat, dydx(1.assignment) post coeflegend
			}

    // Compute 95% confidence intervals for each employment category
    foreach i in 1 2 3 {
        local coef = _b[1.assignment:`i'.empcat]
        local se = _se[1.assignment:`i'.empcat]
        local lb = `coef' - 1.96 * `se'
        local ub = `coef' + 1.96 * `se'
        local ci : di "[" %5.2f `lb' "," %5.2f `ub' "]"
        estadd local ci_`i' `ci'
    }

    // Get margins
    // Test for differences between categories
    test _b[1.assignment:1.empcat] = _b[1.assignment:2.empcat]
    local p12 = round(r(p), 0.001)
    estadd local interaction_12 `p12'
    test _b[1.assignment:2.empcat] = _b[1.assignment:3.empcat]
    local p23 = round(r(p), 0.001)
    estadd local interaction_23 `p23'
    test _b[1.assignment:1.empcat] = _b[1.assignment:3.empcat]
    local p13 = round(r(p), 0.001)
    estadd local interaction_13 `p13'
    // Get sample sizes and control means for each category
    forvalues i = 1/3 {
        count if empcat==`i' & e(sample)
        local n_`i' = r(N)
        estadd local N_`i' `n_`i''
        
        sum `o' if assignment==0 & empcat==`i' & e(sample)
        local cm_`i' = round(r(mean), 0.001)
        estadd local cm_`i' `cm_`i''
    }
}

	use "$data\firm_emppanel.dta", clear 
	keep if wave == 9 & emp_extreme==0 
	preserve
	use "$data\2_firm_regressions_individual_paper.dta", clear
	duplicates drop id, force
	keep id strata_empcat_alt
	tempfile temp
	save `temp'
	restore
	
	merge 1:1 id  using `temp'
	keep if _merge == 3
	

	// Run regressions in loop 
	local c=1 
	foreach o of global outcomes_firm_2  {
		
		local d=`c'+1
		
		* ITT estimate 
		areg `o' i.assignment##i.empcat `o'_2021_std $control mis_`o', a(strata_empcat_alt) vce(cl id)
	    eststo n_`o': margins empcat, dydx(1.assignment) post coeflegend
		
		// Compute 95% confidence intervals for each employment category
		foreach i in 1 2 3 {
        local coef = _b[1.assignment:`i'.empcat]
        local se = _se[1.assignment:`i'.empcat]
        local lb = `coef' - 1.96 * `se'
        local ub = `coef' + 1.96 * `se'
        local ci : di "[" %5.2f `lb' "," %5.2f `ub' "]"
        estadd local ci_`i' `ci'
    }
	
		  // Get margins
    // Test for differences between categories
    test _b[1.assignment:1.empcat] = _b[1.assignment:2.empcat]
    local p12 = round(r(p), 0.001)
    estadd local interaction_12 `p12'
    test _b[1.assignment:2.empcat] = _b[1.assignment:3.empcat]
    local p23 = round(r(p), 0.001)
    estadd local interaction_23 `p23'
    test _b[1.assignment:1.empcat] = _b[1.assignment:3.empcat]
    local p13 = round(r(p), 0.001)
    estadd local interaction_13 `p13'
    // Get sample sizes and control means for each category
    forvalues i = 1/3 {
        count if empcat==`i' & e(sample)
        local n_`i' = r(N)
        estadd local N_`i' `n_`i''
        
        sum `o' if assignment==0 & empcat==`i' & e(sample)
        local cm_`i' = round(r(mean), 0.001)
        estadd local cm_`i' `cm_`i''
	}
	}
	
* ----------------------------
* Small firms (1–3 employees)
* ----------------------------
esttab m_* n_* using "$results\01_tables\Table_4_results_firm.tex", ///
    keep(1.assignment:1.empcat)                                   ///
    b(%9.2f) se(%9.2f)                                            ///
    star(* 0.10 ** 0.05 *** 0.01)                                ///
    stats(ci_1 cm_1 N_1,                                          ///
          fmt(%s %9.2f %9.0f)                                    ///
          labels("95\% CI" "Mean" "\(N\) "))            ///
    noobs label nonumbers nomtitles nonotes                      ///
    prehead("\\multicolumn{1}{l}{\\textbf{Panel B: By number of staff}}\\\\" ///
            "\\textbf{1–3 employees}\\\\")                       ///
    prefoot("\\hline")                                            ///
    postfoot("")                                                 ///
    append
* ----------------------------
* Medium firms (4–6 employees)
* ----------------------------
esttab m_* n_* using "$results\01_tables\Table_4_results_firm.tex", ///
    keep(1.assignment:2.empcat)                                   ///
    b(%9.2f) se(%9.2f)                                            ///
    star(* 0.10 ** 0.05 *** 0.01)                                ///
    stats(ci_2 cm_2 N_2,                                          ///
          fmt(%s %9.2f %9.0f)                                    ///
          labels("95\% CI" "Mean" "\(N\) "))            ///
    noobs label nonumbers nomtitles nonotes                      ///
    prehead("\\textbf{4–6 employees}\\\\")                       ///
    prefoot("\\hline")                                            ///
    postfoot("")                                                 ///
    append

* ----------------------------
* Large firms (6+ employees)
* ----------------------------
esttab m_* n_* using "$results\01_tables\Table_4_results_firm.tex", ///
    keep(1.assignment:3.empcat)                                   ///
    b(%9.2f) se(%9.2f)                                            ///
    star(* 0.10 ** 0.05 *** 0.01)                                ///
    stats(ci_3 cm_3 N_3 interaction_12 interaction_13 interaction_23, ///
          fmt(%s %9.2f %9.0f %9.3f %9.3f %9.3f)                  ///
          labels("95\% CI" "Mean" "\(N\) " "P-val Diff. between group 1 and 2" "P-val Diff. between group 1 and 3" "P-val Diff. between group 2 and 3")) ///
    noobs label nonumbers nomtitles nonotes                      ///
    prehead("\\textbf{6+ employees}\\\\")                         ///
    postfoot("\\hline\\hline\\\\\\multicolumn{13}{l}{\\footnotesize * p<0.10, ** p<0.05, *** p<0.01}\\\\\\end{tabular}") ///
    append
